<?php
  include('header.php');
  $hide_footer = 1;
  if ($_SERVER['REQUEST_METHOD'] != 'GET') {
      echo 'This file cannot be called directly.';
  } else {
      if ($_SESSION['signed_in'] == false) {
          //the user is not an admin
          header('location: signin.php');
          echo '<br /><a href="index.php?id=' . mysql_real_escape_string($_GET['p']) . '">' . $l_back_to . '</a>.';
      } else {
	      //This file will took total 10 queries! To do: improve and optimize queries!
          $result2 = mysql_query("SELECT post_by,post_date,post_cat FROM " . $table_prefix . "posts WHERE post_id =" . mysql_real_escape_string($_GET['p']) . "  ");
          $userid = mysql_fetch_assoc($result2);
          $qqq+=1;
          if ($userid['post_by'] != $_SESSION['user_id'] and $_SESSION['user_level'] < 1) {
              header('location: signin.php');
          } else {
              $to_time = strtotime(date($date_format));
              $from_time = strtotime($userid['post_date']);
              $edit_time = ($to_time - $from_time) / 60;
              if ($edit_time >= $edit_post_time and $_SESSION['user_level'] < 1 and $_SESSION['signed_in'] == true) {
                  echo '<hr><br /><div align="center">' . $l_error_edit_allowed . '</div><br/>';
              } else {
                  $userpostby = $userid['post_by'];
                  $result3 = mysql_query("UPDATE " . $table_prefix . "users SET user_posts=user_posts-1 WHERE user_id =" . $userpostby . "  ");$qqq+=1;
                  $result = mysql_query("DELETE FROM " . $table_prefix . "posts WHERE post_id = {$_GET['p']};");
                  $qqq+=1;
                  $cat_update = mysql_query("UPDATE " . $table_prefix . "categories 
SET 
cat_posts=cat_posts-1 WHERE cat_id =" . $userid['post_cat'] . "") or die(mysql_error("Error #1"));
                  $qqq+=1;
                  //DELETE CACHE
                  $delete_cache = mysql_query("SELECT topic_cat FROM " . $table_prefix . "topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . ""); $qqq+=1;
                  $cache = mysql_fetch_assoc($delete_cache);
                  if ($forum_cache == 1) {
                      if (file_exists("./cache/forum" . $cache['topic_cat'] . ".php")) {
                          unlink("./cache/forum" . $cache['topic_cat'] . ".php");
                      }
                  }
                  // END DELETE CACHE
                  
                  if (!$result || !$result2) {
                      echo 'The post ID: ' . mysql_real_escape_string($_GET['p']) . ' could not be deleted, please try again later.';
                  } else {
                      
                      $sql = "SELECT reply FROM " . $table_prefix . "topics WHERE topics.topic_id = " . mysql_real_escape_string($_GET['t']);
                      $qqq+=1;
                      $result = mysql_query($sql);
                      //check if there is reply...if not delete topic
                      if (mysql_num_rows($result) >= 0) {
                          if (mysql_result($result, 0) <= 0) {
                              $cat_update3 = mysql_query("SELECT topic_cat FROM topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
                              $qqq+=1;
                              $sql2 = mysql_query("DELETE FROM " . $table_prefix . "topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . ";");
                              $qqq+=1;
                              $result4 = mysql_query("DELETE FROM " . $table_prefix . "topics_watch WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . "");
                              $qqq+=1;
                              $tcat = mysql_fetch_assoc($cat_update3);
                              $cat_update33 = mysql_query("SELECT * FROM topics WHERE topic_cat = " . $tcat['topic_cat'] . " ORDER BY topic_last_post_date DESC LIMIT 1");
                              
                              if (mysql_num_rows($cat_update33) >= 1) {
                                  $row_cat = mysql_fetch_assoc($cat_update33);
                                  $cat_last_date = $row_cat['topic_last_post_date'];
                                  $cat_last_postid = $row_cat['topic_id'];
                                  $cat_last_poster = $row_cat['topic_last_poster'];
                                  $cat_last_posterid = $row_cat['topic_last_poster_id'];
                                  $cat_last_tsubject = $row_cat['topic_subject'];
                              }
                              
                              if (mysql_num_rows($cat_update33) <= 0) {
                                  $cat_last_date = "";
                                  $cat_last_postid = 0;
                                  $cat_last_poster = "";
                                  $cat_last_posterid = 0;
                                  $cat_last_tsubject = "";
                              }
                              
                              $cat_update4 = mysql_query("UPDATE " . $table_prefix . "categories 
SET 
cat_last_post_time='" . $cat_last_date . "',
cat_last_post_id='" . $cat_last_postid . "',
cat_last_poster_name='" . $cat_last_poster . "',
cat_last_poster_id=" . $cat_last_posterid . ",
cat_last_post_subject = '" . $cat_last_tsubject . "' 
WHERE cat_id =" . $tcat['topic_cat'] . "") or die(mysql_error());
                              $qqq+=1;
                              header('location: message_topic_deleted.php?f=' . mysql_real_escape_string($_GET['f']) . '&t=' . mysql_real_escape_string($_GET['t']));
                              exit;
                          }
                      }
                      //decrease reply by 1
                      $sql5 = "UPDATE " . $table_prefix . "topics set reply=reply-1 WHERE topic_id=" . mysql_real_escape_string($_GET['t']);
                      $result5 = mysql_query($sql5);
                      $qqq+=1;
                      //Update last post
                      $upd = mysql_query("SELECT post_date, post_by,post_subject FROM " . $table_prefix . "posts WHERE post_topic = " . mysql_real_escape_string($_GET['t']) . " ORDER BY post_date DESC LIMIT 1") or die(mysql_error());$qqq+=1;
                      $update = mysql_fetch_assoc($upd);
                      $qqq+=1;
                      $lastuser = mysql_query("SELECT user_name FROM " . $table_prefix . "users WHERE user_id = '" . $update['post_by'] . "' LIMIT 1") or die(mysql_error());
                      $last_post = mysql_fetch_assoc($lastuser);
                      
					  $cat_update3 = mysql_query("SELECT topic_cat FROM topics WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " LIMIT 1");
					  $tcat = mysql_fetch_assoc($cat_update3);
					  $qqq+=1;
                      $result12 = mysql_query("UPDATE " . $table_prefix . "topics SET topic_last_poster = '" . $last_post['user_name'] . "',topic_last_poster_id = $update[post_by],topic_last_post_date = '" . $update['post_date'] . "' WHERE topic_id = " . mysql_real_escape_string($_GET['t']) . " ") or die(mysql_error());
					  $cat_update4 = mysql_query("
					   UPDATE " . $table_prefix . "categories 
                       SET 
                       cat_last_post_time='$update[post_date]',
                       cat_last_post_id=".mysql_real_escape_string($_GET['t']).",
                       cat_last_poster_name='$last_post[user_name]',
                       cat_last_poster_id=$update[post_by],
                       cat_last_post_subject = '$update[post_subject]' 
                       WHERE cat_id =$tcat[topic_cat]") or die(mysql_error());
                      //END Update last post
$qqq+=1;
                      echo 'Message ID: ' . mysql_real_escape_string($_GET['p']) . ' deleted from Topic ID: ' . mysql_real_escape_string($_GET['t']);
                      header('location: message_deleted.php?f=' . mysql_real_escape_string($_GET['f']) . '&t=' . mysql_real_escape_string($_GET['t']) . '&p=' . mysql_real_escape_string($_GET['p']) . '&page=' . mysql_real_escape_string($_GET['page']));
                  }
              }
          }
      }
  }
  if ($pageTitle = "") {
      $pageTitle = $l_forumname;
  }
  // Get all the page's HTML into a string
  $pageContents = ob_get_contents();
  // Wipe the buffer 
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>